import { defineStore } from "pinia";
import { ref } from "vue";
import axios from "axios";
import { ElMessage } from "element-plus";

const useCarStore = defineStore("useCarStore", () => {
  const tableData = ref([]);
  const baseURL = `http://localhost:8080`;
  let url = '';

  // 车辆信息查询的方法
  const Search = async () => {
    url = `${baseURL}/cars/getList`;

    try {
      const response = await axios.get(url);
      if (response.data && response.data.code === 1) {
        tableData.value = response.data.data || [];
        console.log('查询成功:', response.data.message);
      } else {
        console.error('查询失败:', response.data.message);
        ElMessage({
          type: 'error',
          message: '查询失败，请检查输入条件或稍后再试。',
        });
      }
    } catch (error) {
      console.error('请求出错:', error);
      ElMessage({
        type: 'error',
        message: '服务器请求失败，请检查网络连接或稍后再试。',
      });
    }
  };

  // 查询单个车辆信息的方法
  const SearchOne = async (carNumber) => {
    url = `${baseURL}/cars/get/${carNumber}`;

    try {
      const response = await axios.get(url);
      if (response.data && response.data.code === 1) {
        tableData.value = [response.data.data] || [];
        console.log('查询成功:', response.data.message);
      } else {
        console.error('查询失败:', response.data.message);
        ElMessage({
          type: 'error',
          message: '查询失败，请检查输入条件或稍后再试。',
        });
      }
    } catch (error) {
      console.error('请求出错:', error);
      ElMessage({
        type: 'error',
        message: '服务器请求失败，请检查网络连接或稍后再试。',
      });
    }
  };

  // 删除车辆信息的方法
  const DeleteCar = async (carNumber) => {
    url = `${baseURL}/cars/delete/${carNumber}`;
    try {
      const response = await axios.delete(url);
      if (response.data && response.data.code === 1) {
        tableData.value = tableData.value.filter(car => car.carNumber !== carNumber);
        console.log('删除成功:', response.data.message);
        ElMessage({
          type: 'success',
          message: '删除成功',
        });
      } else {
        console.error('删除失败:', response.data.message);
        ElMessage({
          type: 'error',
          message: '删除失败，请稍后再试。',
        });
      }
    } catch (error) {
      console.error('请求出错:', error);
      ElMessage({
        type: 'error',
        message: '服务器请求失败，请检查网络连接或稍后再试。',
      });
    }
  };

  // 更新车辆信息的方法
  const UpdateCar = async (car) => {
    url = `${baseURL}/cars/update`;
    try {
      const response = await axios.put(url, car);
      if (response.data && response.data.code === 1) {
        const index = tableData.value.findIndex(item => item.carNumber === car.carNumber);
        if (index !== -1) {
          tableData.value[index] = car;
        }
        console.log('更改成功:', response.data.message);
        ElMessage({
          type: 'success',
          message: '更改成功',
        });
      } else {
        console.error('更改失败:', response.data.message);
        ElMessage({
          type: 'error',
          message: '更改失败，请稍后再试。',
        });
      }
    } catch (error) {
      console.error('请求出错:', error);
      ElMessage({
        type: 'error',
        message: '服务器请求失败，请检查网络连接或稍后再试。',
      });
    }
  };

  // 新增车辆信息的方法
  const AddCar = async (car) => {
    url = `${baseURL}/cars/addDate`;
    try {
      const response = await axios.post(url, car);
      if (response.data && response.data.code === 1) {
        tableData.value.push(car);
        console.log('新增成功:', response.data.message);
        ElMessage({
          type: 'success',
          message: '新增成功',
        });
      } else {
        console.error('新增失败:', response.data.message);
        ElMessage({
          type: 'error',
          message: '新增失败，请稍后再试。',
        });
      }
    } catch (error) {
      console.error('请求出错:', error);
      ElMessage({
        type: 'error',
        message: '服务器请求失败，请检查网络连接或稍后再试。',
      });
    }
  };

  return {
    tableData,
    Search,
    SearchOne,
    DeleteCar,
    UpdateCar,
    AddCar
  }
});

export default useCarStore;
